<template>
  <div class="teacher-post-add addButton">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>任课教师管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/teacherManagement/teacherPost/list' }">任课教师职务</el-breadcrumb-item>
      <el-breadcrumb-item>添加任课教师</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="form">
      <el-form ref="form" :model="form" label-width="120px" size="small" :rules="rules">
        <el-form-item label="选择科目:" prop="subject">
          <el-select v-model="form.subject" filterable remote :remote-method="getTeacherList" style="width:230px" placeholder="请选择班级名称">
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任课老师" prop="teacherId">
          <el-select v-model="form.teacherId" filterable remote :remote-method="getTeacherList" style="width:230px" placeholder="请选择班主任">
            <el-option
              v-for="item in teacherList"
              :key="item.id"
              :label="item.user_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button style="background:#ff8b00;color:#fff" @click="onSubmit" :disabled="isDisabled">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>
<script>
export default {
  mounted() {
    this.getSubjectList();
    this.getTeacherList();
  },
  data() {
    return {
      form: {
        subject: "",  //科目
        teacherId: "" // 老师
      },
      subjectList:[], // 科目列表
      teacherList: [],  //教师列表
      rules: {
        subject: [
          { required: true, message: "请选择任课科目", trigger: "blur" },
        ],
        teacherId: [
          { required: true, message: "请选择任课老师", trigger: "blur" },
        ],
      },
      isDisabled: false,
    };
  },
  methods: {
    getTeacherList() {
      this.$api.education.classTeacherList({
        pageNum: 1,
        pageSize: 100,
      })
        .then((res) => {
          this.teacherList = res.data;
        });
    },
    getSubjectList(){
      this.$api.education.subjectList({
        pageNum:1,
        pageSize:100
      }).then((res)=>{
        if(res.code===200){
          console.log(res);
          this.subjectList = res.data.list;
        }
      })
    },
    onSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let params = {
            subject: this.form.subject,
            teacherId: this.form.teacherId
          };
          this.isDisabled = true;
          this.$api.teacherManagement.theTeacherUpdate(params)
            .then((res) => {
              this.$message.success("添加成功");
              this.isDisabled = false;
            })
            .catch(err => {
              this.isDisabled = false;
            });
        }
      });
    },
    resetForm(formName) {
      this.form = {
        name: "",
        classTeacher: ""
      };
      this.isDisabled = false;
    }
  },
};
</script>
<style scoped>
.teacher-post-add {
  padding: 30px;
}

.teacher-post-add .form {
  max-width: 700px;
  margin: 30px 0px;
}

.teacher-post-add .img {
  position: absolute;
  top: 50%;
  left: 50%;
  max-width: 230px;
  max-height: 130px;
  transform: translate(-50%, -50%);
  z-index: 0;
}
</style>
